package com.example.shaketounlock;




import android.app.KeyguardManager;
import android.app.Service;

import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;

import android.widget.Toast;

public class MyService extends Service implements AccelerometerListener {

	
	
	@Override
	public IBinder onBind(Intent intent) {

		Log.d("MyService", "onBind");
		return null;
	}

	@Override
	public void onCreate() {

		Log.d("MyService", "onCreate");

		super.onCreate();
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onStart(Intent intent, int startId) {

		Log.d("MyService", "onStart");
		// Khoi chay
//		Toast.makeText(getBaseContext(), "onResume Accelerometer Started",
//				Toast.LENGTH_LONG).show();

		// Kiem tra thiet bi co ho tro tiem can khong
		if (AccelerometerManager.isSupported(this)) {

			// Start Accelerometer Listening
			AccelerometerManager.startListening(this);
		}
//		SettingData.mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
//		SettingData.mSensor = SettingData.mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
//		SettingData.mSensorManager.registerListener((SensorEventListener) this, SettingData.mSensor,
//				SensorManager.SENSOR_DELAY_NORMAL);
		super.onStart(intent, startId);
	}
//	public void onSensorChanged(SensorEvent event) {
//		if (event.values[0] == 0) {
//			Toast.makeText(getBaseContext(), "Near***",
//					Toast.LENGTH_LONG).show();
//		} else {
//			Toast.makeText(getBaseContext(), "Far**************",
//					Toast.LENGTH_LONG).show();
//		}
//	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		Log.d("MyService", "onStartCommand");
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {

		Log.d("MyService", "onDestroy");
		// Check device supported Accelerometer senssor or not
		if (AccelerometerManager.isListening()) {

			// Start Accelerometer Listening
			AccelerometerManager.stopListening();

//			Toast.makeText(getBaseContext(), "onStop Accelerometer Stoped",
//					Toast.LENGTH_LONG).show();
		}
		super.onDestroy();
	}

	@Override
	public boolean onUnbind(Intent intent) {

		Log.d("MyService", "onUnbind");
		return super.onUnbind(intent);
	}

	@Override
	public void onAccelerationChanged(float x, float y, float z) {

	}

	@Override
	public void onShake(float force) {
//		Toast.makeText(getBaseContext(), "Motion detected", Toast.LENGTH_LONG)
//				.show();
		boolean active = SettingData.deviceManger.isAdminActive(SettingData.compName);
		if (active) {
			
		KeyguardManager myKM = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
		
		if( myKM.inKeyguardRestrictedInputMode()) {
		 //it is locked
			 PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
	            WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
	            wakeLock.acquire();
		} else {
			SettingData.deviceManger.lockNow();
		}
		
		}

	}
	

}